﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows.Input;
using System.Windows;
using SSPharma.Model;
using SSPharma.Command;
using SSPharma.Biz;
using SSPharma.View;
using SSPharma.Common;
using System.Collections.ObjectModel;

namespace SSPharma.ViewModel
{
    public class DangNhapViewModel : ViewModelBase
    {
        #region Fields

        private NguoiSuDung nguoiSuDung;
        private RelayCommand cmdDangNhap;
        private RelayCommand cmdHuy;
        private string errorText;
        private ObservableCollection<DonVi> _donvi;
        private ObservableCollection<ORG> _ORG;
        #endregion

        #region Constructor

        public DangNhapViewModel()
        {
            nguoiSuDung = new NguoiSuDung();
            _donvi = new ObservableCollection<DonVi>();
            _ORG = new ObservableCollection<ORG>();
            Load_DanhMucORG();
            Load_DanhMucDonVi(); 
        }

        #endregion

        #region Properties

        public string NgayLamViec
        {
            get { return nguoiSuDung.NgayLamViec; }
            set
            {
                if (value == nguoiSuDung.NgayLamViec)
                    return;

                nguoiSuDung.NgayLamViec = value;

                base.OnPropertyChanged("NgayLamViec");
            }
        }

        public string MaNguoiSD
        {
            get { return nguoiSuDung.MaNguoiSD; }
            set
            {
                if (value == nguoiSuDung.MaNguoiSD)
                    return;

                nguoiSuDung.MaNguoiSD = value;

                base.OnPropertyChanged("MaNguoiSD");
            }
        }

        public string TenNguoiSD
        {
            get { return nguoiSuDung.TenNguoiSD; }
            set
            {
                if (value == nguoiSuDung.TenNguoiSD)
                    return;

                nguoiSuDung.TenNguoiSD = value;

                base.OnPropertyChanged("TenNguoiSD");
            }
        }

        public string MatKhau
        {
            get { return nguoiSuDung.MatKhau; }
            set
            {
                if (value == nguoiSuDung.MatKhau)
                    return;

                nguoiSuDung.MatKhau = value;

                base.OnPropertyChanged("MatKhau");
            }
        }

        public ICommand DangNhapCommand
        {
            get
            {
                if (cmdDangNhap == null)
                {
                    cmdDangNhap = new RelayCommand(
                        param => this.DangNhap(param));
                }
                return cmdDangNhap;
            }
        }

        public ICommand HuyCommand
        {
            get
            {
                if (cmdHuy == null)
                {
                    cmdHuy = new RelayCommand(
                        param => this.Huy(param));
                }
                return cmdHuy;
            }
        }

        public string ErrorText
        {
            get { return errorText; }
            set
            {
                errorText = value;

                base.OnPropertyChanged("ErrorText");
            }
        }
        public object ORGSelected
        {
            get { return nguoiSuDung.ORGSelected; }
            set
            {
                if (value == nguoiSuDung.ORGSelected)
                    return;

                nguoiSuDung.ORGSelected = value;

                base.OnPropertyChanged("ORGSelected");
            }
        }
        public object DonViSelected
        {
            get { return nguoiSuDung.DonViSelected; }
            set
            {
                if (value == nguoiSuDung.DonViSelected)
                    return;

                nguoiSuDung.DonViSelected =  value;

                base.OnPropertyChanged("DonViSelected");
            }
        }
        public string DonViText
        {
            get { return nguoiSuDung.DonViText; }
            set
            {
                if (value == nguoiSuDung.DonViText)
                    return;

                nguoiSuDung.DonViText = value;

                base.OnPropertyChanged("DonViText");
            }
        }
        public string ORGText
        {
            get { return nguoiSuDung.ORGText; }
            set
            {
                if (value == nguoiSuDung.ORGText)
                    return;

                nguoiSuDung.ORGText = value;

                base.OnPropertyChanged("ORGText");
            }
        }

        public ObservableCollection<DonVi> cboDonVi
        {
            get
            {
                return _donvi;
            }
            set
            {
                _donvi = value;
            }
        }
        public ObservableCollection<ORG> cboORG
        {
            get
            {
                return _ORG;
            }
            set
            {
                _ORG = value;
            }
        }
        #endregion

        #region Public Methods

        public void DangNhap(object sender)
        {
            if (CheckNSD())
            {
                if (nguoiSuDung.DonViSelected != null)
                {
                    Global.DonVi = nguoiSuDung.DonViSelected.GetType().GetProperty("Id").GetValue(nguoiSuDung.DonViSelected, null).ToString();
                }
                if (nguoiSuDung.ORGSelected != null)
                {
                    Global.ORG = nguoiSuDung.ORGSelected.GetType().GetProperty("Id").GetValue(nguoiSuDung.ORGSelected, null).ToString();
                }
                Global.NgayLamViec = !string.IsNullOrEmpty(nguoiSuDung.NgayLamViec) ? LibraryFormatConverter.ConvertStringDDMMYYYYToYYYYMMDD(nguoiSuDung.NgayLamViec) : DateTime.Now;
                Global.UserName = nguoiSuDung.MaNguoiSD;

                ((Window)sender).DialogResult = true;
            }
        }

        public void Huy(object sender)
        {
            ((Window)sender).DialogResult = false;
        }

        #endregion

        #region Private Methods

        public bool CheckNSD()
        {
            List<NguoiSuDung> nsd = BizDangNhap.GetNguoiSDByName(nguoiSuDung.MaNguoiSD);
            if (nsd != null && nsd.Count > 0)
            {
                for (int i = 0; i < nsd.Count; i++)
                {
                    if (string.Compare(nsd[i].MatKhau, nguoiSuDung.MatKhau, StringComparison.Ordinal) == 0)
                    {
                        Common.Global.UserID = nguoiSuDung.MaNguoiSD;
                        return true;
                    }
                }

                ErrorText = "* Tài khoản hay mật khẩu không đúng!";
                return false;
            }
            else
            {
                ErrorText = "* Tài khoản hay mật khẩu không đúng!";
                return false;
            }
        }

        private void Load_DanhMucDonVi()
        {
            SSPharma.Biz.DanhMuc.BizDanhMucDonVi _BizDanhMucDonVi = new Biz.DanhMuc.BizDanhMucDonVi();
            cboDonVi = new ObservableCollection<DonVi>(_BizDanhMucDonVi.fl_DSDanhMuc_DonViTheoOrg(Global.ORG));
            if(cboDonVi.Count>0)
                nguoiSuDung.DonViText = cboDonVi.SingleOrDefault(x => x.Id == Global.DonVi).Name;
        }
        private void Load_DanhMucORG()
        {
            SSPharma.Biz.DanhMuc.BizDanhMucDonVi _BizDanhMucDonVi = new Biz.DanhMuc.BizDanhMucDonVi();
            cboORG = new ObservableCollection<ORG>(_BizDanhMucDonVi.fl_DSDanhMuc_ORG());
            nguoiSuDung.ORGText = cboORG.SingleOrDefault(x => x.Id == Global.ORG).Name;
        }

        #endregion
    }
}
